package com.example.common.dialog;

import com.example.common.ResourceTable;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * @author geyao
 * @date 2021/7/7
 * des:
 */
public class ToastUtil {

    private ToastDialog dialog;

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public ToastUtil(Context context, String msg) {
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_toast, null, false);
        Text text = (Text) layout.findComponentById(ResourceTable.Id_toast_msg);
        text.setText(msg);
        dialog = (ToastDialog) new ToastDialog(context)
                .setContentCustomComponent(layout)
                .setTransparent(true)
                .setAlignment(LayoutAlignment.BOTTOM);
    }
}
